package shiyan6;

public class YiXinZhiCinema extends Thread {

    private static final int START_TICKET_NUMBER = 1430; // 202411671429 -> last four digits + 1
    private static final int TOTAL_TICKETS = 100;
    private static final int END_TICKET_NUMBER = START_TICKET_NUMBER + TOTAL_TICKETS - 1;
    private static final long SLEEP_MILLIS = 300L;

    private static int currentTicketNumber = START_TICKET_NUMBER;

    public YiXinZhiCinema(String windowName) {
        super(windowName);
    }

    @Override
    public void run() {
        while (sellTicket()) {
            try {
                Thread.sleep(SLEEP_MILLIS);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                System.err.println(getName() + " 线程被中断：" + e.getMessage());
                break;
            }
        }
    }

    private boolean sellTicket() {
        synchronized (YiXinZhiCinema.class) {
            if (currentTicketNumber > END_TICKET_NUMBER) {
                return false;
            }
            System.out.println(getName() + " 售出了票号 " + currentTicketNumber);
            currentTicketNumber++;
            return true;
        }
    }
}
